using System;
using UnityEngine;

namespace EventBehaviorTree
{
    [Serializable]
    public class InstantTask : EventNode
    {
        private Action Action = delegate { };
        private StopMode _stopMode;
        private RootNode _root;

        public InstantTask(string stopMode = "None", RootNode root = null)
        {
            _stopMode = (StopMode)Enum.Parse(typeof(StopMode), stopMode);
            _root = root;
        }
        public InstantTask(Action action, string stopMode = "None", RootNode root = null)
        {
            Action = action;
            _stopMode = (StopMode)Enum.Parse(typeof(StopMode), stopMode);
            _root = root;
        }

        public void SetAction(Action action) { Action = action; }

        public override void NodeEvent(NodeMessage nodeMessage)
        {
            if (nodeMessage == NodeMessage.Start)
            {
                //Debug.Log(this + "/ " + Key);
                Action();
                switch (_stopMode)
                {
                    case StopMode.None:
                        SeniorNodeEvent(NodeMessage.Success);
                        return;
                    case StopMode.Abort:
                        SeniorNodeEvent(NodeMessage.REQUEST_Abort);
                        return;
                    case StopMode.Shutdown:
                        _root.NodeEvent(NodeMessage.COMMAND_Stop);
                        return;
                    default:
                        return;
                }
            }
        }
    }
}
